package com.xinqi.modules.chat.chat.group.controller;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.chat.chat.group.dto.req.*;
import com.xinqi.modules.chat.chat.group.dto.rsp.ChatGroupUserResultDTO;
import com.xinqi.modules.chat.chat.group.service.ChatGroupUserService;
import com.xinqi.modules.chat.enums.group.GroupUserStatusEnum;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * APP -- 群组成员
 *
 * @version V1.0
 * @Title:
 * @ClassName: com.xinqi.modules.chat.chat.group.controller.app.ChatGroupUserAppController.java
 * @Description:
 * @Copyright 星齐科技（云南）有限公司 - Powered By 技术部
 * @author: yyzhen
 * @date: 2023/5/19 10:08
 */
@Validated
@RestController
@RequestMapping("/chat-group-user")
@RequiredArgsConstructor
public class ChatGroupUserTempController {
    private final ChatGroupUserService chatGroupUserService;

    @Operation(summary = "设置群-成员")
    @PostMapping("/set-group-user")
    public R<Boolean> setGroupUser(@Validated @RequestBody ChatGroupUserUpdateDTO dto) {
        return R.data(chatGroupUserService.setGroupUser(LoginHelper.getUserId(), dto));
    }

    @Operation(summary = "查看群设置-成员")
    @PostMapping("/get-group-info")
    public R<ChatGroupUserResultDTO> selectByGroupId(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @RequestParam("groupId") String groupId) {
        return R.data(chatGroupUserService.selectByUserIdAndGroupId(LoginHelper.getUserId(), groupId));
    }

    @Operation(summary = "通过群组ID和用户ID查询群成员")
    @PostMapping("/get-group-user")
    public R<ChatGroupUserResultDTO> selectByUserIdAndGroupId(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @RequestParam("groupId") String groupId, @NotNull(message = "user.id.not.black") @RequestParam("userId") Long userId) {
        return R.data(chatGroupUserService.selectByUserIdAndGroupId(userId, groupId));
    }

    @Operation(summary = "用户主动加入群聊")
    @PostMapping("/join-group-user")
    public R<Boolean> joinGroupUser(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @RequestParam("groupId") String groupId) {
        Boolean result = chatGroupUserService.joinGroupUser(groupId, LoginHelper.getXqLoginUser());
        return R.data(result);
    }

    @Operation(summary = "添加群成员")
    @PutMapping("/add-group-user/{groupId}")
    public R<Boolean> addGroupUser(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @PathVariable("groupId") String groupId
        , @Validated @RequestBody AddUserGroupCreateDTO dto) {
        Boolean result = chatGroupUserService.addGroupUser(LoginHelper.getXqLoginUser(), groupId, dto);
        return R.data(result);
    }

    @Operation(summary = "移除群成员")
    @PutMapping("/remove-group-user/{groupId}")
    public R<Boolean> removeGroupUser(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @PathVariable("groupId") String groupId
        , @Validated @RequestBody RemoveUserGroupDTO dto) {
        Boolean result = chatGroupUserService.removeGroupUser(LoginHelper.getXqLoginUser(), groupId, dto);
        return R.data(result);
    }

    @Operation(summary = "管理员设置-添加群管理员")
    @PostMapping("/add-group-manage")
    public R<Boolean> addGroupManage(@Validated @RequestBody AddGroupManageDTO dto) {
        Boolean result = chatGroupUserService.addGroupManage(LoginHelper.getXqLoginUser(), dto);
        return R.data(result);
    }

    @Operation(summary = "管理员设置-移除群管理员")
    @PostMapping("/remove-group-manage")
    public R<Boolean> removeGroupManage(@Validated @RequestBody AddGroupManageDTO dto) {
        Boolean result = chatGroupUserService.removeGroupManage(LoginHelper.getXqLoginUser(), dto);
        return R.data(result);
    }

    @Operation(summary = "管理员设置-查询群管理员列表")
    @GetMapping("/get-manage-list/{groupId}")
    public R<List<ChatGroupUserResultDTO>> getGroupManage(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @PathVariable("groupId") String groupId) {
        List<ChatGroupUserResultDTO> result = chatGroupUserService.findGroupManage(groupId);
        return R.data(result);
    }

    @Operation(summary = "管理员-查询审核列表")
    @GetMapping("/get-manage-auto-list/{groupId}")
    public R<List<ChatGroupUserResultDTO>> getGroupManageAutoList(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @PathVariable("groupId") String groupId) {
        List<ChatGroupUserResultDTO> result = chatGroupUserService.getGroupManageAutoList(LoginHelper.getUserId(), groupId);
        return R.data(result);
    }

    @Operation(summary = "查询成员列表")
    @PostMapping("/get-user-list")
    public R<List<ChatGroupUserResultDTO>> getUserList(@RequestBody ChatGroupUserQueryDTO query) {
        query.setOperationUserId(LoginHelper.getUserId());
        List<ChatGroupUserResultDTO> result = chatGroupUserService.getUserList(query);
        return R.data(result);
    }

    @Operation(summary = "查询成员列表 -- 分页")
    @GetMapping("/get-user-page")
    public R<PageInfo<ChatGroupUserResultDTO>> getUserPage(Param pageable, @Validated ChatGroupUserQueryDTO query) {
        query.setOperationUserId(LoginHelper.getUserId());
        return R.ok(chatGroupUserService.getUserPage(pageable, query));
    }

    @Operation(summary = "退出群聊")
    @PutMapping("/exit-group/{groupId}")
    public R<Boolean> exitGroup(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @PathVariable("groupId") String groupId) {
        Boolean result = chatGroupUserService.exitGroup(LoginHelper.getUserId(), groupId);
        return R.data(result);
    }

    @Operation(summary = "添加消息免打扰关注成员")
    @PutMapping("/add-group-follow/{groupId}")
    public R<Boolean> addGroupFollow(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @PathVariable("groupId") String groupId
        , @RequestBody AddUserGroupCreateDTO dto) {
        Boolean result = chatGroupUserService.addGroupFollow(LoginHelper.getUserId(), groupId, dto);
        return R.data(result);
    }

    @Operation(summary = "查询消息免打扰关注成员")
    @GetMapping("/get-group-follow/{groupId}")
    public R<List<ChatGroupUserResultDTO>> getGroupFollow(@NotEmpty(message = "{chat.chat.groupId.not.blank}") @PathVariable("groupId") String groupId) {
        List<ChatGroupUserResultDTO> result = chatGroupUserService.getGroupFollow(LoginHelper.getUserId(), groupId);
        return R.data(result);
    }

    @Operation(summary = "拒绝/通过入群申请")
    @PostMapping("/examine")
    public R<Void> examine(@RequestBody ChatGroupUsersExamineDTO dto) {
        if (Objects.equals(GroupUserStatusEnum.ONE, dto.getStatus())) {
            if (chatGroupUserService.examineUserToGroup(LoginHelper.getXqLoginUser(), dto)) {
                return R.ok(MessageUtils.message("chat.group.user.examine.success"));
            } else {
                return R.error(MessageUtils.message("chat.group.user.examine.fail"));
            }
        } else if (Objects.equals(GroupUserStatusEnum.TWO, dto.getStatus())) {
            if (chatGroupUserService.refuseUserToGroup(LoginHelper.getXqLoginUser(), dto)) {
                return R.ok(MessageUtils.message("chat.group.user.examine.success"));
            } else {
                return R.error(MessageUtils.message("chat.group.user.examine.fail"));
            }
        }
        return R.ok();
    }

    @Operation(summary = "批量拒绝/通过入群申请")
    @PostMapping("/batch-examine")
    public R<Void> batchExamine(@RequestBody ChatGroupUsersBatchExamineDTO dto) {
        if (Objects.equals(GroupUserStatusEnum.ONE, dto.getStatus())) {
            Boolean examine = chatGroupUserService.examineUserToGroup(LoginHelper.getXqLoginUser(), dto);
            if (examine) {
                return R.ok(MessageUtils.message("chat.group.user.examine.success"));
            } else {
                return R.error(MessageUtils.message("chat.group.user.examine.fail"));
            }
        } else if (Objects.equals(GroupUserStatusEnum.TWO, dto.getStatus())) {
            Boolean refuse = chatGroupUserService.refuseUserToGroup(LoginHelper.getXqLoginUser(), dto);
            if (refuse) {
                return R.ok(MessageUtils.message("chat.group.user.examine.success"));
            } else {
                return R.error(MessageUtils.message("chat.group.user.examine.fail"));
            }
        }
        return R.ok();
    }

}
